<template>
	<view class="container">
		<uni-segmented-control class="header" :current="current" :values="values" styleType="text" activeColor="#184795"
			@clickItem="changeItem">
		</uni-segmented-control>
		<!-- 学校要闻 -->
		<view v-if="current===0">
			<uni-section v-for="item in importantNews" :key="item.id" :title="item.title"
				:subTitle="moment(Number.parseInt(item.publishTimestamp)).format('YYYY-MM-DD')"
				@click="jumpToHandler(item.articleUrl)"></uni-section>
		</view>
		<!-- 学校动态 -->
		<view v-else-if="current===1">
			<uni-section v-for="item in trends" :key="item.id" :title="item.title"
				:subTitle="moment(Number.parseInt(item.publishTimestamp)).format('YYYY-MM-DD')"
				@click="jumpToHandler(item.articleUrl)"></uni-section>
		</view>
		<!-- 媒体报道 -->
		<view v-else-if="current===2">
			<uni-section v-for="item in media" :key="item.id" :title="item.title"
				:subTitle="moment(Number.parseInt(item.publishTimestamp)).format('YYYY-MM-DD')"
				@click="jumpToHandler(item.articleUrl)"></uni-section>
		</view>
		<!-- 通知公告 -->
		<view v-else-if="current===3">
			<uni-section v-for="item in notice" :key="item.id" :title="item.title"
				:subTitle="moment(Number.parseInt(item.publishTimestamp)).format('YYYY-MM-DD')"
				@click="jumpToHandler(item.articleUrl)"></uni-section>
		</view>
		<!-- 部门资讯 -->
		<view v-else-if="current===4">
			<uni-section v-for="item in dept" :key="item.id" :title="item.title"
				:subTitle="moment(Number.parseInt(item.publishTimestamp)).format('YYYY-MM-DD')"
				@click="jumpToHandler(item.articleUrl)"></uni-section>
		</view>
	</view>
</template>

<script setup>
	import {
		onBeforeMount,
		reactive,
		ref,
	} from "vue";
	import moment from "moment";

	const current = ref(0);
	const values = reactive(["学校要闻", "学校动态", "媒体报道", "通知公告","部门资讯"]);
	// 学校要闻
	const importantNews = ref([]);
	// 学校动态
	const trends = ref([]);
	//媒体报道
	const media = ref([]);
	// 通知公告
	const notice = ref([]);
	// 部门资讯
	const dept = ref([]);

	defineExpose({
		refresh
	})

	function changeItem(e) {
		current.value = e.currentIndex;
	}

	// 跳转
	function jumpToHandler(url) {
		uni.navigateTo({
			url: `/pages/webview/webview?url=${url}`,
		});
	}

	// 刷新
	function refresh() {
		// 学校要闻
		uni.request({
			url: "https://i-wzut-edu-cn-s.webvpn.wzut.edu.cn:8118/mnews/mobile/getPortalArticleList16.rst?_p=YXM9MiZ0PTUmZD0xMzQmcD0xJmY9NDgmbT1OJg__&callback=jQuery171019081474533146192_1671777914880&siteId=2&columnId=3&pageSize=6&beginIndex=0&_=1671777915644&sf_request_type=ajax"
		}).then(({
			data
		}) => {
			importantNews.value = JSON.parse(data.split("(")[1].split(")")[0]).data.articles;
			// console.log(importantNews.value);
		})
		// 学校动态
		uni.request({
			url: "https://i-wzut-edu-cn-s.webvpn.wzut.edu.cn:8118/mnews/mobile/getPortalArticleList16.rst?_p=YXM9MiZ0PTUmZD0xMzQmcD0xJmY9NDgmbT1OJg__&callback=jQuery171019081474533146192_1671777914881&siteId=2&columnId=6&pageSize=6&beginIndex=0&_=1671777915826&sf_request_type=ajax"
		}).then(({
			data
		}) => {
			trends.value = JSON.parse(data.split("(")[1].split(")")[0]).data.articles;
			// console.log(trends.value);
		})
		// 媒体报道
		uni.request({
			url: "https://i-wzut-edu-cn-s.webvpn.wzut.edu.cn:8118/mnews/mobile/getPortalArticleList16.rst?_p=YXM9MiZ0PTUmZD0xMzQmcD0xJmY9NDgmbT1OJg__&callback=jQuery171019081474533146192_1671777914882&siteId=2&columnId=4&pageSize=6&beginIndex=0&_=1671777916044&sf_request_type=ajax"
		}).then(({
			data
		}) => {
			media.value = JSON.parse(data.split("(")[1].split(")")[0]).data.articles;
			// console.log(trends.value);
		})
		// 通知公告
		uni.request({
			url: "https://i-wzut-edu-cn-s.webvpn.wzut.edu.cn:8118/mnews/mobile/getPortalArticleList16.rst?_p=YXM9MiZ0PTUmZD0xMzQmcD0xJmY9NDgmbT1OJg__&callback=jQuery171019081474533146192_1671777914883&siteId=2&columnId=5&pageSize=6&beginIndex=0&_=1671777916213&sf_request_type=ajax"
		}).then(({
			data
		}) => {
			notice.value = JSON.parse(data.split("(")[1].split(")")[0]).data.articles;
			// console.log(trends.value);
		})
		// 部门资讯
		uni.request({
			url: "https://i-wzut-edu-cn-s.webvpn.wzut.edu.cn:8118/mnews/mobile/getPortalArticleList16.rst?_p=YXM9MiZ0PTUmZD0xMzQmcD0xJmY9NDgmbT1OJg__&callback=jQuery171019081474533146192_1671777914884&siteId=2&columnId=7&pageSize=6&beginIndex=0&_=1671777916358&sf_request_type=ajax"
		}).then(({
			data
		}) => {
			dept.value = JSON.parse(data.split("(")[1].split(")")[0]).data.articles;
			// console.log(trends.value);
		})
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx;
		width: 100%;

		.header {
			// width: 70%;
		}
	}
</style>
